suppressWarnings(library(R2HTML))

# retrieve args
Args <- commandArgs(TRUE)

#Read in data
statdata <- read.csv(Args[3], header=TRUE, sep=",")

#Copy Args
csResponses<-Args[4]
transformation<-Args[5]
firstCat<-Args[6]
secondCat<-Args[7]
thirdCat<-Args[8]
fourthCat<-Args[9]
mean<-Args[10]
N<-Args[11]
StDev<-Args[12]
Variances<-Args[13]
StErr<-Args[14]
MinMax<-Args[15]
MedianQuartile<-Args[16]
CoeffVariation<-Args[17]
confidenceLimits<-Args[18]
CIval2<-as.numeric(Args[19])
CIval<-CIval2/100
NormalProbabilityPlot<-Args[20]
ByCategoriesAndOverall<-Args[21]


#Setup the html file and associated css file
htmlFile <- sub(".csv", ".html", Args[3]); #determine the file name of the html file
.HTML.file = htmlFile
cssFile <- "r2html.css"
cssFile <- paste("'",cssFile,"'", sep="") #need to enclose in quotes when path has spaces in it
HTMLCSS(CSSfile = cssFile)

#Output HTML header
HTML.title("<bf>SilveR Summary Statistics", HR=1, align="left")
HTML.title("</bf> ", HR=2, align="left")

#Breakdown the list of responses

resplist<-c()
tempChanges <-strsplit(csResponses, ",")
expectedChanges <- c(0)
for (i in 1:length(tempChanges[[1]])) 
{
	expectedChanges [length(expectedChanges )+1] = (tempChanges[[1]][i]) 
} 
for (j in 1:(length(expectedChanges)-1)) 
{
	resplist[j] = expectedChanges[j+1] 
} 
resplength<-length(resplist)

# Create the normal probability plot titles
#STB - Sept 2010

resplistqq<-resplist

for (i in 1:10)
{
resplistqq<-sub("ivs_tilde_ivs"	,"~", resplistqq) 
resplistqq<-sub("ivs_star_ivs"	,"*", resplistqq) 
resplistqq<-sub("ivs_plus_ivs"	,"+", resplistqq) 
resplistqq<-sub("ivs_sp_ivs"	," ", resplistqq) 
resplistqq<-sub("ivs_ob_ivs"	,"(", resplistqq) 
resplistqq<-sub("ivs_cb_ivs"	,")", resplistqq) 
resplistqq<-sub("ivs_div_ivs"	,"/", resplistqq) 
resplistqq<-sub("ivs_pc_ivs"	,"%", resplistqq) 
resplistqq<-sub("ivs_hash_ivs"	,"#", resplistqq) 
resplistqq<-sub("ivs_pt_ivs"	,".", resplistqq) 
resplistqq<-sub("ivs_hyphen_ivs","-", resplistqq) 
resplistqq<-sub("ivs_at_ivs"	,"@", resplistqq) 
resplistqq<-sub("ivs_colon_ivs"	,":", resplistqq) 
resplistqq<-sub("ivs_exclam_ivs","!", resplistqq) 
resplistqq<-sub("ivs_quote_ivs"	,"`", resplistqq) 
resplistqq<-sub("ivs_pound_ivs"	,"£", resplistqq) 
resplistqq<-sub("ivs_dollar_ivs","$", resplistqq) 
resplistqq<-sub("ivs_hat_ivs"	,"^", resplistqq) 
resplistqq<-sub("ivs_amper_ivs"	,"&", resplistqq) 
resplistqq<-sub("ivs_obrace_ivs","{", resplistqq) 
resplistqq<-sub("ivs_cbrace_ivs","}", resplistqq) 
resplistqq<-sub("ivs_semi_ivs"	,";", resplistqq) 
resplistqq<-sub("ivs_pipe_ivs"	,"|", resplistqq) 
resplistqq<-sub("ivs_slash_ivs"	,"\\", resplistqq) 
resplistqq<-sub("ivs_osb_ivs"	,"[", resplistqq) 
resplistqq<-sub("ivs_csb_ivs"	,"]", resplistqq) 
resplistqq<-sub("ivs_eq_ivs"	,"=", resplistqq) 
resplistqq<-sub("ivs_lt_ivs"	,"<", resplistqq) 
resplistqq<-sub("ivs_gt_ivs"	,">", resplistqq) 
resplistqq<-sub("ivs_dblquote_ivs"	,"\"", resplistqq) 
}


# Loop to run the analysis

#start of do loop x
#STB 06 September 2010
if ( mean != "N" || N != "N" || StDev != "N" || Variances != "N" || StErr != "N" || MinMax != "N" || MedianQuartile != "N" || CoeffVariation != "N" || confidenceLimits!= "N")
{

for (i in 1:resplength)
{
	csResponses <- resplist[i]


	if (firstCat != "NULL" || secondCat != "NULL" || thirdCat != "NULL" || fourthCat != "NULL")
	{
		#Analysis if categorisation factors selected
		
		#Setting up parameters and vectors
		
		length <- length(unique(levels(as.factor(statdata$catfact))))
		tablenames<-c(levels(as.factor(statdata$catfact)))
		table<-c(1:length)
		for (i in 1:length)
		{
			table[i]=" "
		}
		vectormean <-c(1:length)
		vectorN <-c(1:length)
		vectorStDev <-c(1:length)
		vectorVariances <-c(1:length)
		vectorStErr <-c(1:length)
		vectorMin <-c(1:length)
		vectorMax <-c(1:length)
		vectorMedian <-c(1:length)
		vectorLQ <-c(1:length)
		vectorUQ <-c(1:length)
		vectorCoeffVariation <-c(1:length)
		vectorUCI <-c(1:length)
		vectorLCI <-c(1:length)
		
		for (i in 1:length)
		{
			sub<-subset(statdata, statdata$catfact == unique(levels(as.factor(statdata$catfact)))[i])
			sub2<-data.frame(sub)
			if (mean == "Y" )
			{
				vectormean[i]=mean(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE)
			}
			if (N == "Y" )
			{
				tempy<-na.omit(eval(parse(text = paste("sub2$", csResponses))))
				vectorN[i]=length(tempy)
			}
			if (StDev == "Y" )
			{
				vectorStDev[i]=sd(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE)
			}
			if (Variances == "Y" )
			{
				vectorVariances[i]=var(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE)
			}
			if (StErr == "Y" )
			{
				tempy<-na.omit(eval(parse(text = paste("sub2$", csResponses))))
				vectorStErr[i]=sd(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE) / (length(tempy))**(0.5)
			}
			if (MinMax == "Y" )
			{
				vectorMin[i]=suppressWarnings(min(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE))
			}
			if (MinMax == "Y" )
			{
				vectorMax[i]=suppressWarnings(max(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE))
			}
			if (MedianQuartile == "Y" )
			{
				vectorMedian[i]=median(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE)
			}
			if (MedianQuartile == "Y" )
			{
				vectorLQ[i]=quantile(eval(parse(text = paste("sub2$", csResponses))), 0.25, type=2,na.rm = TRUE)
			}
			if (MedianQuartile == "Y" )
			{
				vectorUQ[i]=quantile(eval(parse(text = paste("sub2$", csResponses))), 0.75, type=2,na.rm = TRUE)
			}
			if (CoeffVariation == "Y" )
			{
				vectorCoeffVariation[i]=100*sd(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE) / mean(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE)
			}
			if (confidenceLimits == "Y" )
			{
				tempy<-na.omit(eval(parse(text = paste("sub2$", csResponses))))
				vectorLCI[i]= mean(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE)-qt(1-(1-CIval)/2, (length(tempy)-1))*sd(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE) / (length(tempy))**(0.5)
			}
			if (confidenceLimits == "Y" )
			{
				tempy<-na.omit(eval(parse(text = paste("sub2$", csResponses))))
				vectorUCI[i]= mean(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE)+qt(1-(1-CIval)/2, (length(tempy)-1))*sd(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE) / (length(tempy))**(0.5)
			}
		}

		#Generating final table dataset

		if (mean == "Y" )
		{
			vectormean<-format(round(vectormean, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectormean)
		}
		if (N == "Y" )
		{
			#vectorN<-format(round(vectormean, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorN)
		}
		if (StDev == "Y" )
		{
			vectorStDev<-format(round(vectorStDev, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorStDev)
		}
		if (Variances == "Y" )
		{
			vectorVariances<-format(round(vectorVariances, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorVariances)
		}
		if (StErr == "Y" )
		{
			vectorStErr<-format(round(vectorStErr, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorStErr)
		}
		if (MinMax == "Y" )
		{
			vectorMin<-format(round(vectorMin, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorMin)
		}
		if (MinMax == "Y" )
		{
			vectorMax<-format(round(vectorMax, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorMax)
		}
		if (MedianQuartile == "Y" )
		{
			vectorMedian<-format(round(vectorMedian, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorMedian)
		}
		if (MedianQuartile == "Y" )
		{
			vectorLQ<-format(round(vectorLQ, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorLQ)
		}
		if (MedianQuartile == "Y" )
		{
			vectorUQ<-format(round(vectorUQ, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorUQ)
		}
		if (CoeffVariation == "Y" )
		{
			vectorCoeffVariation<-format(round(vectorCoeffVariation, 1), nsmall=1, scientific=FALSE)
			table<-cbind(table,vectorCoeffVariation)
		}
		if (confidenceLimits == "Y" )
		{
			vectorLCI<-format(round(vectorLCI, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorLCI)
		}
		if (confidenceLimits == "Y" )
		{
			vectorUCI<-format(round(vectorUCI, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorUCI)
		}

		#creating final output table
		#Generating blank line

		if (dim(table)[2]==2)
		{
			header<-c(" "," ")
		}
		if (dim(table)[2]==3)
		{
			header<-c(" "," "," ")
		}
		if (dim(table)[2]==4)
		{
			header<-c(" "," "," "," ")
		}
		if (dim(table)[2]==5)
		{	
			header<-c(" "," "," "," "," ")
		}
		if (dim(table)[2]==6)
		{
			header<-c(" "," "," "," "," "," ")
		}
		if (dim(table)[2]==7)
		{
			header<-c(" "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==8)
		{
			header<-c(" "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==9)
		{
			header<-c(" "," "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==10)
		{
			header<-c(" "," "," "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==11)
		{
			header<-c(" "," "," "," "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==12)
		{
			header<-c(" "," "," "," "," "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==13)
		{
			header<-c(" "," "," "," "," "," "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==14)
		{
			header<-c(" "," "," "," "," "," "," "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==15)
		{
			header<-c(" "," "," "," "," "," "," "," "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==16)
		{
			header<-c(" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ")
		}
		
		table2<-rbind(header, table)

		#Generating column names
		temp6<-c(" ")
		if (mean == "Y" )
		{
			hed1<-c("Mean")
			temp6<-cbind(temp6,hed1)
		}
		if (N == "Y" )
		{
			hed2<-c("N")
			temp6<-cbind(temp6,hed2)
		}
		if (StDev == "Y" )
		{
			hed3<-c("StDev")
			temp6<-cbind(temp6,hed3)
		}
		if (Variances == "Y" )
		{
			hed4<-c("Variance")
			temp6<-cbind(temp6,hed4)
		}
		if (StErr == "Y" )
		{
			hed5<-c("StErr")
			temp6<-cbind(temp6,hed5)
		}
		if (MinMax == "Y" )
		{
			hed6<-c("Min")
			temp6<-cbind(temp6,hed6)
		}
		if (MinMax == "Y" )
		{
			hed7<-c("Max")
			temp6<-cbind(temp6,hed7)
		}
		if (MedianQuartile == "Y" )
		{
			hed8<-c("Median")
			temp6<-cbind(temp6,hed8)
		}
		if (MedianQuartile == "Y" )
		{
			hed9<-c("Lower Quartile")
			temp6<-cbind(temp6,hed9)
		}
		if (MedianQuartile == "Y" )
		{
			hed10<-c("Upper Quartile")
			temp6<-cbind(temp6,hed10)
		}
		if (CoeffVariation == "Y" )
		{
			hed11<-c("%CV")
			temp6<-cbind(temp6,hed11)
		}
		if (confidenceLimits == "Y" )
		{
			CIlow<-paste("Lower ", 100*CIval, sep="")
			CIlow<-paste(CIlow , "% CI", sep="")
			hed12<-c(CIlow)
			temp6<-cbind(temp6,hed12)
		}
		if (confidenceLimits == "Y" )
		{
			CIhigh<-paste("Upper ", 100*CIval, sep="")
			CIhigh<-paste(CIhigh, "% CI", sep="")
			hed13<-c(CIhigh)
			temp6<-cbind(temp6,hed13)
		}

		colnames(table2)<-temp6
		rownms<-c("Categorisation Factor levels")
		for(i in 1:length)
		{
			rownms[i+1]<-levels(as.factor(statdata$catfact))[i]
		}
		row.names(table2)<-rownms

		add<-paste(c("Summary statistics for "), csResponses, sep="")
		add<-paste(add, " categorised by ", sep="")
		

		if (firstCat !="NULL" && secondCat =="NULL" && thirdCat == "NULL" && fourthCat=="NULL")
		{
			add<-paste(add, firstCat, sep="")
		} else
		if (firstCat =="NULL" && secondCat !="NULL" && thirdCat == "NULL" && fourthCat=="NULL")
		{
			add<-paste(add, secondCat, sep="")
		} else
		if (firstCat =="NULL" && secondCat =="NULL" && thirdCat != "NULL" && fourthCat=="NULL")
		{
			add<-paste(add, thirdCat, sep="")
		} else
		if (firstCat =="NULL" && secondCat =="NULL" && thirdCat == "NULL" && fourthCat!="NULL")
		{
			add<-paste(add, fourthCat, sep="")
		} 

		if (firstCat !="NULL" && secondCat !="NULL" && thirdCat == "NULL" && fourthCat=="NULL")
		{
			add<-paste(add, firstCat, sep="")
			add<-paste(add, " and ", sep="")
			add<-paste(add, secondCat, sep="")
		} 

		if (firstCat !="NULL" && secondCat =="NULL" && thirdCat != "NULL" && fourthCat=="NULL")
		{
			add<-paste(add, firstCat, sep="")
			add<-paste(add, " and ", sep="")
			add<-paste(add, thirdCat, sep="")
		} 

		if (firstCat !="NULL" && secondCat =="NULL" && thirdCat == "NULL" && fourthCat!="NULL")
		{
			add<-paste(add, firstCat, sep="")
			add<-paste(add, " and ", sep="")
			add<-paste(add, fourthCat, sep="")
		} 

		if (firstCat =="NULL" && secondCat !="NULL" && thirdCat != "NULL" && fourthCat=="NULL")
		{
			add<-paste(add, secondCat, sep="")
			add<-paste(add, " and ", sep="")
			add<-paste(add, thirdCat, sep="")
		} 

		if (firstCat =="NULL" && secondCat !="NULL" && thirdCat == "NULL" && fourthCat!="NULL")
		{
			add<-paste(add, secondCat, sep="")
			add<-paste(add, " and ", sep="")
			add<-paste(add, fourthCat, sep="")
		} 

		if (firstCat =="NULL" && secondCat =="NULL" && thirdCat != "NULL" && fourthCat!="NULL")
		{
			add<-paste(add, thirdCat, sep="")
			add<-paste(add, " and ", sep="")
			add<-paste(add, fourthCat, sep="")
		} 

		if (firstCat =="NULL" && secondCat !="NULL" && thirdCat != "NULL" && fourthCat!="NULL")
		{
			add<-paste(add, secondCat, sep="")
			add<-paste(add, ", ", sep="")
			add<-paste(add, thirdCat, sep="")
			add<-paste(add, " and ", sep="")
			add<-paste(add, fourthCat, sep="")
		} 

		if (firstCat !="NULL" && secondCat !="NULL" && thirdCat != "NULL" && fourthCat=="NULL")
		{
			add<-paste(add, firstCat, sep="")
			add<-paste(add, ", ", sep="")
			add<-paste(add, secondCat, sep="")
			add<-paste(add, " and ", sep="")
			add<-paste(add, thirdCat, sep="")
		} 

		if (firstCat !="NULL" && secondCat !="NULL" && thirdCat == "NULL" && fourthCat!="NULL")
		{
			add<-paste(add, firstCat, sep="")
			add<-paste(add, ", ", sep="")
			add<-paste(add, secondCat, sep="")
			add<-paste(add, " and ", sep="")
			add<-paste(add, fourthCat, sep="")
		} 

		if (firstCat !="NULL" && secondCat =="NULL" && thirdCat != "NULL" && fourthCat!="NULL")
		{
			add<-paste(add, firstCat, sep="")
			add<-paste(add, ", ", sep="")
			add<-paste(add, thirdCat, sep="")
			add<-paste(add, " and ", sep="")
			add<-paste(add, fourthCat, sep="")
		} 

		if (firstCat !="NULL" && secondCat !="NULL" && thirdCat != "NULL" && fourthCat!="NULL")
		{
			add<-paste(add, firstCat, sep="")
			add<-paste(add, ", ", sep="")
			add<-paste(add, secondCat, sep="")
			add<-paste(add, ", ", sep="")
			add<-paste(add, thirdCat, sep="")
			add<-paste(add, " and ", sep="")
			add<-paste(add, fourthCat, sep="")
		} 
		HTMLbr()
		HTML.title(add, HR=0, align="left")
		HTML(table2, , align="left" , classfirstline="second")
		HTML.title("</bf> ", HR=2, align="left")
	}


	#Analysis without any categorisation factor
	if ((firstCat == "NULL" && secondCat == "NULL" && thirdCat == "NULL" && fourthCat == "NULL")|| ByCategoriesAndOverall=="Y")
	{
		#Analysis if categorisation factors selected

		#Setting up parameters and vectors
		length <- 1
		tablenames<-c(" ")
		table<-c(1:length)
		for (i in 1:length)
		{
			table[i]=" "
		}
		vectormean <-c(1:length)
		vectorN <-c(1:length)
		vectorStDev <-c(1:length)
		vectorVariances <-c(1:length)
		vectorStErr <-c(1:length)
		vectorMin <-c(1:length)
		vectorMax <-c(1:length)
		vectorMedian <-c(1:length)
		vectorLQ <-c(1:length)
		vectorUQ <-c(1:length)
		vectorCoeffVariation <-c(1:length)
		vectorUCI <-c(1:length)
		vectorLCI <-c(1:length)
	#	vectorLPercentBoundaries <-c(1:length)
	#	vectorUPercentBoundaries <-c(1:length)
	
		for (i in 1:length)
		{
			sub<-statdata
			sub2<-data.frame(sub)
	
			if (mean == "Y" )
			{
				vectormean[i]=mean(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE)
			}
			if (N == "Y" )
			{
				tempy<-na.omit(eval(parse(text = paste("sub2$", csResponses))))
				vectorN[i]=length(tempy)
			}
			if (StDev == "Y" )
			{
				vectorStDev[i]=sd(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE)
			}
			if (Variances == "Y" )
			{
				vectorVariances[i]=var(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE)
			}
			if (StErr == "Y" )
			{
				tempy<-na.omit(eval(parse(text = paste("sub2$", csResponses))))
				vectorStErr[i]=sd(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE) / (length(tempy))**(0.5)
			}
			if (MinMax == "Y" )
			{
				vectorMin[i]=suppressWarnings(min(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE))
			}
			if (MinMax == "Y" )
			{
				vectorMax[i]=suppressWarnings(max(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE))
			}
			if (MedianQuartile == "Y" )
			{
				vectorMedian[i]=median(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE)
			}
			if (MedianQuartile == "Y" )
			{
				vectorLQ[i]=quantile(eval(parse(text = paste("sub2$", csResponses))), 0.25, type=2,na.rm = TRUE)
#				vectorLQ[i]=fivenum(eval(parse(text = paste("sub2$", csResponses))), na.rm = TRUE)[2]
			}
			if (MedianQuartile == "Y" )
			{
				vectorUQ[i]=quantile(eval(parse(text = paste("sub2$", csResponses))), 0.75, type=2,na.rm = TRUE)
#				vectorUQ[i]=fivenum(eval(parse(text = paste("sub2$", csResponses))), na.rm = TRUE)[4]
			}
			if (CoeffVariation == "Y" )
			{
				vectorCoeffVariation[i]=100*sd(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE) / mean(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE)
			}
			if (confidenceLimits == "Y" )
			{
				tempy<-na.omit(eval(parse(text = paste("sub2$", csResponses))))
				vectorLCI[i]= mean(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE)-qt(1-(1-CIval)/2, (length(tempy)-1))*sd(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE) / (length(tempy))**(0.5)
			}
			if (confidenceLimits == "Y" )
			{
				tempy<-na.omit(eval(parse(text = paste("sub2$", csResponses))))
				vectorUCI[i]= mean(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE)+qt(1-(1-CIval)/2, (length(tempy)-1))*sd(eval(parse(text = paste("sub2$", csResponses))), na.rm=TRUE) / (length(tempy))**(0.5)
			}
		}

		#Generating final table dataset
		if (mean == "Y" )
		{
			vectormean<-format(round(vectormean, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectormean)
		}
		if (N == "Y" )
		{
			#vectorN<-format(round(vectormean, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorN)
		}
		if (StDev == "Y" )
		{
			vectorStDev<-format(round(vectorStDev, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorStDev)
		}
		if (Variances == "Y" )
		{
			vectorVariances<-format(round(vectorVariances, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorVariances)
		}
		if (StErr == "Y" )
		{
			vectorStErr<-format(round(vectorStErr, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorStErr)
		}
		if (MinMax == "Y" )
		{
			vectorMin<-format(round(vectorMin, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorMin)
		}
		if (MinMax == "Y" )
		{
			vectorMax<-format(round(vectorMax, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorMax)
		}
		if (MedianQuartile == "Y" )
		{
			vectorMedian<-format(round(vectorMedian, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorMedian)
		}
		if (MedianQuartile == "Y" )
		{
			vectorLQ<-format(round(vectorLQ, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorLQ)
		}
		if (MedianQuartile == "Y" )
		{
			vectorUQ<-format(round(vectorUQ, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorUQ)
		}
		if (CoeffVariation == "Y" )
		{
			vectorCoeffVariation<-format(round(vectorCoeffVariation, 1), nsmall=1, scientific=FALSE)
			table<-cbind(table,vectorCoeffVariation)
		}
		if (confidenceLimits == "Y" )
		{
			vectorLCI<-format(round(vectorLCI, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorLCI)
		}
		if (confidenceLimits == "Y" )
		{
			vectorUCI<-format(round(vectorUCI, 4), nsmall=4, scientific=FALSE)
			table<-cbind(table,vectorUCI)
		}
	
		#creating final output table
		
		#Generating blank line
		
		if (dim(table)[2]==2)
		{
			header<-c(" "," ")
		}
		if (dim(table)[2]==3)
		{
			header<-c(" "," "," ")
		}
		if (dim(table)[2]==4)
		{
			header<-c(" "," "," "," ")
		}
		if (dim(table)[2]==5)
		{
			header<-c(" "," "," "," "," ")
		}
		if (dim(table)[2]==6)
		{
			header<-c(" "," "," "," "," "," ")
		}
		if (dim(table)[2]==7)
		{
			header<-c(" "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==8)
		{
			header<-c(" "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==9)
		{
			header<-c(" "," "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==10)
		{
			header<-c(" "," "," "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==11)
		{
			header<-c(" "," "," "," "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==12)
		{
			header<-c(" "," "," "," "," "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==13)
		{
			header<-c(" "," "," "," "," "," "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==14)
		{
			header<-c(" "," "," "," "," "," "," "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==15)
		{
			header<-c(" "," "," "," "," "," "," "," "," "," "," "," "," "," "," ")
		}
		if (dim(table)[2]==16)
		{
			header<-c(" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ")
		}
		
		table2<-rbind(header, table)
		
		#Generating column names
		temp6<-c(" ")
		
		if (mean == "Y" )
		{
			hed1<-c("Mean")
			temp6<-cbind(temp6,hed1)
		}
		if (N == "Y" )
		{
			hed2<-c("N")
			temp6<-cbind(temp6,hed2)
		}
		if (StDev == "Y" )
		{
			hed3<-c("StDev")
			temp6<-cbind(temp6,hed3)
		}
		if (Variances == "Y" )
		{
			hed4<-c("Variance")
			temp6<-cbind(temp6,hed4)
		}
		if (StErr == "Y" )
		{
			hed5<-c("StErr")
			temp6<-cbind(temp6,hed5)
		}
		if (MinMax == "Y" )
		{
			hed6<-c("Min")
			temp6<-cbind(temp6,hed6)
		}
		if (MinMax == "Y" )
		{
			hed7<-c("Max")
			temp6<-cbind(temp6,hed7)
		}
		if (MedianQuartile == "Y" )
		{
			hed8<-c("Median")
			temp6<-cbind(temp6,hed8)
		}
		if (MedianQuartile == "Y" )
		{
			hed9<-c("Lower Quartile")
			temp6<-cbind(temp6,hed9)
		}
		if (MedianQuartile == "Y" )
		{
			hed10<-c("Upper Quartile")
			temp6<-cbind(temp6,hed10)
		}
		if (CoeffVariation == "Y" )
		{
			hed11<-c("%CV")
			temp6<-cbind(temp6,hed11)
		}
		if (confidenceLimits == "Y" )
		{
			CIlow<-paste("Lower ", 100*CIval, sep="")
			CIlow<-paste(CIlow , "% CI", sep="")
			hed12<-c(CIlow)
			temp6<-cbind(temp6,hed12)
		}
		if (confidenceLimits == "Y" )
		{
			CIhigh<-paste("Upper ", 100*CIval, sep="")
			CIhigh<-paste(CIhigh, "% CI", sep="")
			hed13<-c(CIhigh)
			temp6<-cbind(temp6,hed13)
		}

		colnames(table2)<-temp6

		rownms<-c(" ")
		for(i in 1:length)
		{
			rownms[i+1]<-csResponses
		}
		
		row.names(table2)<-rownms


		if ((firstCat == "NULL" && secondCat == "NULL" && thirdCat == "NULL" && fourthCat == "NULL"))
		{
			HTMLbr()
			add<-paste(c("Summary statistics for "), csResponses, sep="")
			HTML.title(add, HR=0, align="left")
			HTML(table2, , align="left" , classfirstline="second")
			HTML.title("</bf> ", HR=2, align="left")
		
		} else 
		if ((firstCat != "NULL" || secondCat != "NULL" || thirdCat != "NULL" || fourthCat != "NULL") && ByCategoriesAndOverall=="Y")
		{
			HTMLbr()
			add<-paste(c("Overall summary statistics, ignoring the categorisation factor(s), for "), csResponses, sep="")
			HTML.title(add, HR=0, align="left")
			HTML(table2, , align="left" , classfirstline="second")
			HTML.title("</bf> ", HR=2, align="left")
		}
	}
}

#HTML.title("<bf> ", HR=2, align="left")

if (transformation != "None")
{
	HTMLbr()
	HTML.title("<bf>Transformation", HR=2, align="left")

	add2<-paste(c("Responses "), transformation, sep="")
	add2<-paste(add2, " transformed prior to analysis.", sep="")
	HTML.title(add2, HR=0, align="left")
}

#end of do loop x
#STB 06 September 2010
}

#Normal probability plot
# Added 01 September 2010
#STB


if (NormalProbabilityPlot != "N")
{
	HTMLbr()
	HTML.title("<bf>Normal probability plot", HR=2, align="left")


#first plot
	csResponses <- resplist[1]
	csResponsesqq <- resplistqq[1]
	normPlot <- sub(".html", "normplot.jpg", htmlFile)
	jpeg(normPlot)
	adda<-paste(c("Normal probability plot for "), csResponsesqq, sep="")
	qqnorm( eval(parse(text = paste("statdata$", csResponses))) ,main=adda)
	qqline(eval(parse(text = paste("statdata$", csResponses))), col="red", lty="dotted")
	void<-HTMLInsertGraph(GraphFileName=sub("[A-Z0-9a-z,:,\\\\]*App_Data[\\\\]","", normPlot), Align="left")

#second plot
	if (resplength > 1)
	{
		csResponses <- resplist[2]
		csResponsesqq <- resplistqq[2]
		normPlot2 <- sub(".html", "normplot2.jpg", htmlFile)
		jpeg(normPlot2)
		adda<-paste(c("Normal probability plot for "), csResponsesqq, sep="")
		qqnorm( eval(parse(text = paste("statdata$", csResponses))) ,main=adda)
		qqline(eval(parse(text = paste("statdata$", csResponses))), col="red", lty="dotted")
		void<-HTMLInsertGraph(GraphFileName=sub("[A-Z0-9a-z,:,\\\\]*App_Data[\\\\]","", normPlot2), Align="left")
	}
#third plot
	if (resplength > 2)
	{
		csResponses <- resplist[3]
		csResponsesqq <- resplistqq[3]
		normPlot3 <- sub(".html", "normplot3.jpg", htmlFile)
		jpeg(normPlot3)
		adda<-paste(c("Normal probability plot for "), csResponsesqq, sep="")
		qqnorm( eval(parse(text = paste("statdata$", csResponses))) ,main=adda)
		qqline(eval(parse(text = paste("statdata$", csResponses))), col="red", lty="dotted")
		void<-HTMLInsertGraph(GraphFileName=sub("[A-Z0-9a-z,:,\\\\]*App_Data[\\\\]","", normPlot3), Align="left")
	}
#fourth plot
	if (resplength > 3)
	{
		csResponses <- resplist[4]
		csResponsesqq <- resplistqq[4]
		normPlot4 <- sub(".html", "normplot4.jpg", htmlFile)
		jpeg(normPlot4)
		adda<-paste(c("Normal probability plot for "), csResponsesqq, sep="")
		qqnorm( eval(parse(text = paste("statdata$", csResponses))) ,main=adda)
		qqline(eval(parse(text = paste("statdata$", csResponses))), col="red", lty="dotted")
		void<-HTMLInsertGraph(GraphFileName=sub("[A-Z0-9a-z,:,\\\\]*App_Data[\\\\]","", normPlot4), Align="left")
	}
#fifth plot
	if (resplength > 4)
	{
		csResponses <- resplist[5]
		csResponsesqq <- resplistqq[5]
		normPlot5 <- sub(".html", "normplot5.jpg", htmlFile)
		jpeg(normPlot5)
		adda<-paste(c("Normal probability plot for "), csResponsesqq, sep="")
		qqnorm( eval(parse(text = paste("statdata$", csResponses))) ,main=adda)
		qqline(eval(parse(text = paste("statdata$", csResponses))), col="red", lty="dotted")
		void<-HTMLInsertGraph(GraphFileName=sub("[A-Z0-9a-z,:,\\\\]*App_Data[\\\\]","", normPlot5), Align="left")
	}
#sixth plot
	if (resplength > 5)
	{
		csResponses <- resplist[6]
		csResponsesqq <- resplistqq[6]
		normPlot6 <- sub(".html", "normplot6.jpg", htmlFile)
		jpeg(normPlot6)
		adda<-paste(c("Normal probability plot for "), csResponsesqq, sep="")
		qqnorm( eval(parse(text = paste("statdata$", csResponses))) ,main=adda)
		qqline(eval(parse(text = paste("statdata$", csResponses))), col="red", lty="dotted")
		void<-HTMLInsertGraph(GraphFileName=sub("[A-Z0-9a-z,:,\\\\]*App_Data[\\\\]","", normPlot6), Align="left")
	}
#seventh plot
	if (resplength > 6)
	{
		csResponses <- resplist[7]
		csResponsesqq <- resplistqq[7]
		normPlot7 <- sub(".html", "normplot7.jpg", htmlFile)
		jpeg(normPlot7)
		adda<-paste(c("Normal probability plot for "), csResponsesqq, sep="")
		qqnorm( eval(parse(text = paste("statdata$", csResponses))) ,main=adda)
		qqline(eval(parse(text = paste("statdata$", csResponses))), col="red", lty="dotted")
		void<-HTMLInsertGraph(GraphFileName=sub("[A-Z0-9a-z,:,\\\\]*App_Data[\\\\]","", normPlot7), Align="left")
	}
#eigth plot
	if (resplength > 7)
	{
		csResponses <- resplist[8]
		csResponsesqq <- resplistqq[8]
		normPlot8 <- sub(".html", "normplot8.jpg", htmlFile)
		jpeg(normPlot8)
		adda<-paste(c("Normal probability plot for "), csResponsesqq, sep="")
		qqnorm( eval(parse(text = paste("statdata$", csResponses))) ,main=adda)
		qqline(eval(parse(text = paste("statdata$", csResponses))), col="red", lty="dotted")
		void<-HTMLInsertGraph(GraphFileName=sub("[A-Z0-9a-z,:,\\\\]*App_Data[\\\\]","", normPlot8), Align="left")
	}
#ninth plot
	if (resplength > 8)
	{
		csResponses <- resplist[9]
		csResponsesqq <- resplistqq[9]
		normPlot9 <- sub(".html", "normplot9.jpg", htmlFile)
		jpeg(normPlot9)
		adda<-paste(c("Normal probability plot for "), csResponsesqq, sep="")
		qqnorm( eval(parse(text = paste("statdata$", csResponses))) ,main=adda)
		qqline(eval(parse(text = paste("statdata$", csResponses))), col="red", lty="dotted")
		void<-HTMLInsertGraph(GraphFileName=sub("[A-Z0-9a-z,:,\\\\]*App_Data[\\\\]","", normPlot9), Align="left")
	}
#tenth plot
	if (resplength > 9)
	{
		csResponses <- resplist[10]
		csResponsesqq <- resplistqq[10]
		normPlot10 <- sub(".html", "normplot10.jpg", htmlFile)
		jpeg(normPlot10)
		adda<-paste(c("Normal probability plot for "), csResponsesqq, sep="")
		qqnorm( eval(parse(text = paste("statdata$", csResponses))) ,main=adda)
		qqline(eval(parse(text = paste("statdata$", csResponses))), col="red", lty="dotted")
		void<-HTMLInsertGraph(GraphFileName=sub("[A-Z0-9a-z,:,\\\\]*App_Data[\\\\]","", normPlot10), Align="left")
	}
#other plots
	if (resplength > 10)
	{
	HTML.title("<bf> ", HR=2, align="left")
	HTML.title("<bf>Normal probability plots are only produced for the first ten selected responses.", HR=0, align="left")
	}



	HTML.title("<bf> ", HR=2, align="left")
	HTML.title("<bf>Tip: Check that the points lie along the dotted line. If not then the data may be non-normally distributed.", HR=0, align="left")


	if (firstCat == "NULL" && secondCat == "NULL" && thirdCat == "NULL" && fourthCat == "NULL")
		{
			add2<-c(" ")
			HTML.title(add2, HR=0, align="left")
		} else if ((firstCat != "NULL" && secondCat == "NULL" && thirdCat == "NULL" && fourthCat == "NULL")||(firstCat == "NULL" && secondCat != "NULL" && thirdCat == "NULL" && fourthCat == "NULL")||(firstCat == "NULL" && secondCat == "NULL" && thirdCat != "NULL" && fourthCat == "NULL")||(firstCat == "NULL" && secondCat == "NULL" && thirdCat == "NULL" && fourthCat != "NULL"))
		{
HTML.title("<bf> ", HR=2, align="left")
			add3<-c("Warning: This Normal probability plot does not take into account the categorisation factor. If you wish to assess normality after taking the categorisation factor into account, please use the plot in the Single Measures Parametric Analysis module.")
			HTML.title(add3, HR=0, align="left")
		} else {
			add4<-c("Warning: This Normal probability plot does not take into account the categorisation factors. If you wish to assess normality after taking the categorisation factors into account, please use the plot in the Single Measures Parametric Analysis module.")
HTML.title("<bf> ", HR=2, align="left")
			HTML.title(add4, HR=0, align="left")
			}
}









HTMLbr()
HTML.title("<bf>R references", HR=2, align="left")

HTML.title("<bf> ", HR=2, align="left")
	HTML.title("<bf>   R Development Core Team (2008). R: A language and environment for
	statistical computing. R Foundation for Statistical Computing,
	Vienna, Austria. ISBN 3-900051-07-0, URL http://www.R-project.org.", HR=0, align="left")

HTML.title("<bf> ", HR=2, align="left")
	HTML.title("<bf>    Lecoutre, Eric (2003). The R2HTML Package. R News, Vol 3. N. 3, Vienna, Austria.
	", HR=0, align="left")
